package com.zhuoyian.zyalibrary.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author Wiley
 */
public class SP {
	private static SP instance;
	/**
	 * 文件KEY
	 * */
	private final static String FILE_NAME = "ZYASDKPOIEJMFKL##@!!!";

	private SP() {
	}

	/**
	 * 获取单例对象
	 * 
	 * @return
	 */
	public static SP getInstance() {
		if (instance == null) {
			instance = new SP();
		}
		return instance;
	}

	/**
	 * 写入指定KEY的字符串内容
	 * 
	 * @param context
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean put(Context context, String key, String value) {
		SharedPreferences settings = context.getSharedPreferences(FILE_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		return editor.commit();
	}

	/**
	 * 写入指定KEY的布尔内容
	 * 
	 * @param context
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean put(Context context, String key, boolean value) {
		SharedPreferences settings = context.getSharedPreferences(FILE_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		return editor.commit();
	}

	/**
	 * 获取指定KEY的布尔内容
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean getBoolean(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(FILE_NAME, 0);
		return settings.getBoolean(key, false);
	}

	/**
	 * 写入指定KEY的长整形内容
	 * 
	 * @param context
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean put(Context context, String key, long value) {
		SharedPreferences settings = context.getSharedPreferences(FILE_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(key, value);
		return editor.commit();
	}

	/**
	 * 获取指定KEY的字符串内容
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static String get(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(FILE_NAME, 0);
		return settings.getString(key, "");
	}

	/**
	 * 获取指定KEY的长整形内容
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static long getLong(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(FILE_NAME, 0);
		return settings.getLong(key, 0L);
	}

	/**
	 * 清除指定KEY内容
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean clear(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(FILE_NAME, 0);
		return settings.edit().clear().commit();
	}

}
